/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.economy.web.form.discount;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import ar.uba.fi.posgrado.common.exception.PosgradoRuntimeException;
import ar.uba.fi.posgrado.common.util.CommonUtils;
import ar.uba.fi.posgrado.economy.model.Discount;

/**
 * Form para {@link Discount}
 * 
 * @author nicolas.gonzalez
 */
@SuppressWarnings("serial")
public class DiscountForm extends ActionForm {

    private Long id;
    private String description;
    private Double percentage;
    private Double value;
    private String validFrom;
    private String validTo;
    
    private List<Discount> discounts = new ArrayList<Discount>();

    /**
     * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
     */
    @Override
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        ActionErrors actionErrors = new ActionErrors();
        
        if (this.value == 0d && percentage == 0d) {
            actionErrors.add("discount.value", new ActionMessage("discount.valueOrPercentage.empty"));
            actionErrors.add("discount.percentage", new ActionMessage("discount.valueOrPercentage.empty"));
        }
        if (this.value == 0d) {
            // El campo llenado debe ser porcentaje. Valido q sea entre 1 y 99
            if (this.percentage < 0d || this.percentage > 99.99d) {
                actionErrors.add("discount.percentage", new ActionMessage("discount.percentage.range"));
            }
        }
        if (this.percentage == 0d) {
            // El campo llenado debe ser valor. Valido q sea positivo
            if (this.value < 0d) {
                actionErrors.add("discount.value", new ActionMessage("discount.value.range"));
            }
        }
        if (this.value < 0d) {
            actionErrors.add("discount.value", new ActionMessage("discount.value.range"));
        }
        if (this.percentage < 0d) {
            actionErrors.add("discount.percentage", new ActionMessage("discount.percentage.range"));
        }
        if (this.value > 0d && this.percentage > 0d) {
            actionErrors.add("discount.percentage", new ActionMessage("discount.valueOrPercentage"));
        }
        
        try {
            Date initialDate = CommonUtils.toDateFromStringStandard(validFrom);
            Date finalDate = CommonUtils.toDateFromStringStandard(validTo);
            
            if (initialDate.compareTo(finalDate) > 0) {
                actionErrors.add("discount.valids", new ActionMessage("discount.dates.invalid"));
            }
        } catch (PosgradoRuntimeException e) {
            actionErrors.add("discount.valids", new ActionMessage("discount.dates.parseError"));
        }
        
        return actionErrors;
    }


    /**
     * @return the description
     */
    public String getDescription() {
        return this.description;
    }


    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }


    /**
     * @return the percentage
     */
    public Double getPercentage() {
        return this.percentage;
    }


    /**
     * @param percentage the percentage to set
     */
    public void setPercentage(Double percentage) {
        this.percentage = percentage;
    }


    /**
     * @return the value
     */
    public Double getValue() {
        return this.value;
    }


    /**
     * @param value the value to set
     */
    public void setValue(Double value) {
        this.value = value;
    }


    /**
     * @return the validFrom
     */
    public String getValidFrom() {
        return this.validFrom;
    }


    /**
     * @param validFrom the validFrom to set
     */
    public void setValidFrom(String validFrom) {
        this.validFrom = validFrom;
    }


    /**
     * @return the validTo
     */
    public String getValidTo() {
        return this.validTo;
    }


    /**
     * @param validTo the validTo to set
     */
    public void setValidTo(String validTo) {
        this.validTo = validTo;
    }


    /**
     * @return the id
     */
    public Long getId() {
        return this.id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }


    /**
     * @return the discounts
     */
    public List<Discount> getDiscounts() {
        return this.discounts;
    }


    /**
     * @param discounts the discounts to set
     */
    public void setDiscounts(List<Discount> discounts) {
        this.discounts = discounts;
    }

    
    
    
}
